package packAutomatricula;

import java.awt.EventQueue;

import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JList;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;

public class JDElegirAsig extends JDialog implements Observer {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JList<String> listaAsignaturasDisp;
	private JList<String> listaAsignaturasElegidas;
	private JLabel lblAsignaturasDisponibles;
	private JLabel lblAsignaturasElegidas;
	private JButton btnEscoger;
	private JButton btnEliminar;
	private JButton btnMatricularse;
	private JLabel lblPasado;
	private Alumno miAlumno;
	DefaultListModel<String> dlmDisp = new DefaultListModel();
	DefaultListModel<String> dlmSele = new DefaultListModel();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JDElegirAsig dialog = new JDElegirAsig(null);
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the dialog.
	 */
	public JDElegirAsig(Alumno pAlumno) {
		miAlumno = pAlumno;
		miAlumno.matricularAlumno();
		initialize();
		
	}
	private void initialize() {
		setModal(true);
		setBounds(100, 100, 463, 343);
		
		Universidad.getMiUniversidad().addObserver(this);
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(getListaAsignaturasDisp(), GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addGroup(groupLayout.createSequentialGroup()
											.addGap(33)
											.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
												.addComponent(getBtnEscoger())
												.addComponent(getBtnEliminar())))
										.addGroup(groupLayout.createSequentialGroup()
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addComponent(getBtnMatricularse()))))
								.addComponent(getLblAsignaturasDisponibles()))
							.addGap(10)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(getLblAsignaturasElegidas())
								.addComponent(getListaAsignaturasElegidas(), GroupLayout.PREFERRED_SIZE, 151, GroupLayout.PREFERRED_SIZE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(41)
							.addComponent(getLblPasado())))
					.addContainerGap(13, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(getLblAsignaturasDisponibles())
						.addComponent(getLblAsignaturasElegidas()))
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(58)
							.addComponent(getBtnEscoger())
							.addGap(18)
							.addComponent(getBtnEliminar())
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(getBtnMatricularse()))
						.addGroup(groupLayout.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(getListaAsignaturasDisp(), GroupLayout.PREFERRED_SIZE, 202, GroupLayout.PREFERRED_SIZE)
								.addComponent(getListaAsignaturasElegidas(), GroupLayout.PREFERRED_SIZE, 202, GroupLayout.PREFERRED_SIZE))))
					.addGap(18)
					.addComponent(getLblPasado())
					.addContainerGap(26, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
		
		
		//Cargamos las asignaturas en la lista
		Iterator<String> itr = miAlumno.getListaAsignaturasActual().getIterador();
		while (itr.hasNext()){
			dlmDisp.addElement(itr.next());
		}
		listaAsignaturasDisp.setModel(dlmDisp);
		
		
	}
	private JList<String> getListaAsignaturasDisp() {
		if (listaAsignaturasDisp == null) {
			listaAsignaturasDisp = new JList<String>();
		}
		return listaAsignaturasDisp;
	}
	private JList<String> getListaAsignaturasElegidas() {
		if (listaAsignaturasElegidas == null) {
			listaAsignaturasElegidas = new JList<String>();
		}
		return listaAsignaturasElegidas;
	}
	private JLabel getLblAsignaturasDisponibles() {
		if (lblAsignaturasDisponibles == null) {
			lblAsignaturasDisponibles = new JLabel("Asignaturas Disponibles:");
		}
		return lblAsignaturasDisponibles;
	}
	private JLabel getLblAsignaturasElegidas() {
		if (lblAsignaturasElegidas == null) {
			lblAsignaturasElegidas = new JLabel("Asignaturas Elegidas:");
		}
		return lblAsignaturasElegidas;
	}
	
	private JButton getBtnEscoger() {
		if (btnEscoger == null) {
			btnEscoger = new JButton(">>");
			btnEscoger.addMouseListener(new MouseAdapter() {
				@Override
				public void mousePressed(MouseEvent e) {
					
					String seleccionado = (String) listaAsignaturasDisp.getSelectedValue();
					dlmSele.addElement(seleccionado);
					listaAsignaturasElegidas.setModel(dlmSele);
					//Eliminamos el elemento
					dlmDisp.removeElement(seleccionado);
					listaAsignaturasDisp.setModel(dlmDisp);
				}
			});
		}
		return btnEscoger;
	}
	
	private JButton getBtnEliminar() {
		if (btnEliminar == null) {
			btnEliminar = new JButton("<<");
			btnEliminar.addMouseListener(new MouseAdapter() {
				@Override
				public void mousePressed(MouseEvent e) {
					String seleccionado = (String) listaAsignaturasElegidas.getSelectedValue();
					dlmDisp.addElement(seleccionado);
					listaAsignaturasDisp.setModel(dlmDisp);
					//Eliminamos el elemento
					dlmSele.removeElement(seleccionado);
					listaAsignaturasElegidas.setModel(dlmSele);
				}
			});
		}
		return btnEliminar;
	}
	private JButton getBtnMatricularse() {
		if (btnMatricularse == null) {
			btnMatricularse = new JButton("Matricularse");
			btnMatricularse.addMouseListener(new MouseAdapter() {
				@Override
				public void mousePressed(MouseEvent e) {
					int misCreditos = 0;
					JDMatriculado mat = new JDMatriculado();
					JDMasAsig pregunta = new JDMasAsig();
					ListaNombresAsignatura lista = new ListaNombresAsignatura();
					
					
					for (int i = 0; i < dlmSele.size(); i++) {
						Asignatura miAsig = ListaAsignaturas.getMiListaAsignaturas().buscarAsignaturaPorId(dlmSele.get(i));
						misCreditos = misCreditos + miAsig.getNumCreditos();
						lista.anadirAsignatura(miAsig.getIdAsignatura());
					}
					if (misCreditos < 90){
						pregunta.setVisible(true);
						if(!pregunta.getValor()){
							miAlumno.getExpediente().anadirAsignaturasExpediente(lista);
							miAlumno.setEstaMatriculado(true);
							
							
							mat.setVisible(true);
							
							//Terminar matriculacion
							//la variable con el alumno es miAlumno
						}
					}else if (misCreditos > 90) {
						lblPasado.setVisible(true);
					}else if (misCreditos == 90){
						//Terminar matriculacion
						miAlumno.getExpediente().anadirAsignaturasExpediente(lista);
						miAlumno.setEstaMatriculado(true);
						mat.setVisible(true);
					}

					
				}
			});
		}
		return btnMatricularse;
	}
	private JLabel getLblPasado() {
		if (lblPasado == null) {
			lblPasado = new JLabel("Te has pasado de n\u00FAmero de cr\u00E9ditos, quita alguna asignatura.");
			lblPasado.setForeground(Color.RED);
			lblPasado.setVisible(false);
		}
		return lblPasado;
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		JDexpediente exp = new JDexpediente(miAlumno);
		if(miAlumno.isEstaMatriculado()){
			exp.setVisible(true);
			
		}
		
	}
}
